package com.ayf.payment.game.api.service.impl;

import com.ayf.payment.game.api.dto.MerchantAlipayConfigDTO;
import com.ayf.payment.game.api.entity.MerchantAlipayConfig;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.mapper.MerchantAlipayConfigMapper;
import com.ayf.payment.game.api.service.MerchantAlipayConfigService;
import com.swwx.charm.commons.lang.utils.BeanConvertUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class MerchantAlipayConfigServiceImpl implements MerchantAlipayConfigService {

    @Resource
    private MerchantAlipayConfigMapper merchantAlipayConfigMapper;

    @Override
    public MerchantAlipayConfigDTO getConfigByMerchantId(Integer merchantId) {
        MerchantAlipayConfig merchantAlipayConfig = new MerchantAlipayConfig();
        merchantAlipayConfig.setMerchantId(merchantId);
        MerchantAlipayConfig config = merchantAlipayConfigMapper.selectOne(merchantAlipayConfig);
        if (config == null) {
            merchantAlipayConfig.setStatus(0);
            merchantAlipayConfig.setSelfFlag(1);
            merchantAlipayConfigMapper.insertSelective(merchantAlipayConfig);
            config = merchantAlipayConfigMapper.selectOne(merchantAlipayConfig);
        }
        return BeanConvertUtils.convert(config, MerchantAlipayConfigDTO.class);
    }

    @Override
    public boolean updateConfig(MerchantAlipayConfigDTO dto) {
        if (dto.getMerchantId() == null) {
            throw new ParameterException("MerchantId不能为空");
        }
        MerchantAlipayConfigDTO configByMerchantId = getConfigByMerchantId(dto.getMerchantId());
        dto.setId(configByMerchantId.getId());
        MerchantAlipayConfig alipayConfig = BeanConvertUtils.convert(dto, MerchantAlipayConfig.class);
        return merchantAlipayConfigMapper.updateByPrimaryKeySelective(alipayConfig) > 0;
    }

    @Override
    public MerchantAlipayConfigDTO getAlipayConfigByMerchantId(Integer merchantId,Integer selfFlag) {
        MerchantAlipayConfig merchantAlipayConfig = new MerchantAlipayConfig();
        merchantAlipayConfig.setMerchantId(merchantId);
        MerchantAlipayConfig config = merchantAlipayConfigMapper.selectOne(merchantAlipayConfig);
        if (config==null){
            merchantAlipayConfig.setStatus(0);
            merchantAlipayConfig.setSelfFlag(1);
            merchantAlipayConfigMapper.insertSelective(merchantAlipayConfig);
            config = merchantAlipayConfigMapper.selectOne(merchantAlipayConfig);
        } else {
            if (selfFlag !=null && Integer.valueOf(0).equals(selfFlag)){// 交易之前使用的是平台企业支付宝配置
                merchantAlipayConfig = new MerchantAlipayConfig();
                merchantAlipayConfig.setMerchantId(0);
                config = merchantAlipayConfigMapper.selectOne(merchantAlipayConfig);
            } else {
                if (Integer.valueOf(0).equals(config.getSelfFlag())){ // 使用平台企业支付宝配置
                    merchantAlipayConfig = new MerchantAlipayConfig();
                    merchantAlipayConfig.setMerchantId(0);
                    config = merchantAlipayConfigMapper.selectOne(merchantAlipayConfig);
                }
            }
        }
        return BeanConvertUtils.convert(config, MerchantAlipayConfigDTO.class);
    }
}